package com.feihong.dubbo.model.util.result;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;

/**
 * 响应结果统一封装工具类
 *
 * @author zhufeihong
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Result<T> implements Serializable {

    /**
     * 真正的返回数据
     */
    T model;

    /**
     * 响应状态码
     */
    private Integer code;

    /**
     * 提示信息
     */
    private String msg;

    /**
     * 调用是否成功
     */
    private boolean success;

    /**
     * 请求成功方法组
     */
    public static <T> Result<T> success() {
        ResultStatusEnum statusEnum = ResultStatusEnum.SUCCESS;
        return new Result<>(null, statusEnum.getCode(), statusEnum.getMsg(), true);
    }

    public static <T> Result<T> success(ResultStatusEnum statusEnum) {
        return new Result<>(null, statusEnum.getCode(), statusEnum.getMsg(), true);
    }

    public static <T> Result<T> success(String msg) {
        return new Result<>(null, null, msg, true);
    }

    public static <T> Result<T> success(Integer code) {
        return new Result<>(null, code, null, true);
    }

    public static <T> Result<T> success(Integer code, String msg) {
        return new Result<>(null, code, msg, true);
    }

    public static <T> Result<T> successWith(T model) {
        ResultStatusEnum statusEnum = ResultStatusEnum.SUCCESS;
        return new Result<>(model, statusEnum.getCode(), statusEnum.getMsg(), true);
    }

    public static <T> Result<T> successWith(T model, Integer code) {
        return new Result<>(model, code, null, true);
    }

    public static <T> Result<T> successWith(T model, String msg) {
        return new Result<>(model, ResultStatusEnum.SUCCESS.getCode(), msg, true);
    }

    public static <T> Result<T> successWith(T model, Integer code, String msg) {
        return new Result<>(model, code, msg, true);
    }

    public static <T> Result<T> successWith(T model, ResultStatusEnum statusEnum) {
        return new Result<>(model, statusEnum.getCode(), statusEnum.getMsg(), true);
    }


    /**
     * 请求失败方法组
     */
    public static <T> Result<T> failed() {
        ResultStatusEnum statusEnum = ResultStatusEnum.FAILED_REQUEST;
        return new Result<>(null, statusEnum.getCode(), statusEnum.getMsg(), false);
    }

    public static <T> Result<T> failed(String msg) {
        return new Result<>(null, ResultStatusEnum.FAILED_REQUEST.getCode(), msg, false);
    }

    public static <T> Result<T> failed(Integer code) {
        return new Result<>(null, code, null, false);
    }

    public static <T> Result<T> failed(ResultStatusEnum statusEnum) {
        return new Result<>(null, statusEnum.getCode(), statusEnum.getMsg(), false);
    }

    public static <T> Result<T> failed(Integer code, String msg) {
        return new Result<>(null, code, msg, false);
    }

    public static <T> Result<T> failedWith(T model) {
        ResultStatusEnum statusEnum = ResultStatusEnum.FAILED_REQUEST;
        return new Result<>(model, statusEnum.getCode(), statusEnum.getMsg(), false);
    }

    public static <T> Result<T> failedWith(T model, ResultStatusEnum statusEnum) {
        return new Result<>(model, statusEnum.getCode(), statusEnum.getMsg(), false);
    }

    public static <T> Result<T> failedWith(T model, Integer code, String msg) {
        return new Result<>(model, code, msg, false);
    }

    public static <T> Result<T> failedWith(T model, String msg) {
        return new Result<>(model, ResultStatusEnum.FAILED_REQUEST.getCode(), msg, false);
    }

    public static <T> Result<T> failedWith(T model, Integer code) {
        return new Result<>(model, code, null, false);
    }
}
